package com.example.selfcontrol.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.selfcontrol.entity.video.Video;
import com.example.selfcontrol.entity.video.VideoAddress;
import com.example.selfcontrol.mapper.VideoAddressMapper;
import com.example.selfcontrol.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/api")
public class VideoController {
    @Autowired
    private VideoAddressMapper videoAddressMapper ;

    @Autowired
    private VideoMapper videoMapper ;

    @GetMapping(value = "/video.m3u8", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    // 接口要以.m3u8结尾,前端m3u8播放器才能正确识别资源
    public ResponseEntity<byte[]> getM3U8(@RequestParam("video_id") String video_id , @RequestParam("eps_id") String eps_id) throws IOException {
//        String m3u8FilePath = "D:\\ffmpeg_exp\\build\\playlist.m3u8"; // 替换为实际M3U8文件的路径
        QueryWrapper<VideoAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("video_id",video_id).eq("eps_id",eps_id);
        VideoAddress videoAddress = videoAddressMapper.selectOne(wrapper) ;
        if(videoAddress != null) {
//            String m3u8FilePath = videoAddress.getAddress() + "\\playlist.m3u8";
            String m3u8FilePath = videoAddress.getAddress() + "/playlist.m3u8";     // Linux上的路径
            System.out.println(m3u8FilePath);
            Path m3u8Path = Paths.get(m3u8FilePath);
            byte[] m3u8Content = Files.readAllBytes(m3u8Path);
            return ResponseEntity.ok()
                    .contentLength(m3u8Content.length)
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(m3u8Content);
        }

        return ResponseEntity.notFound().build();
    }

    // 返回ts文件内容
    // TODO 分片每次请求都要查询filepath有点蠢啊
    @GetMapping("/{fileName:.+\\.ts}")
    public ResponseEntity<byte[]> getTSFile(@PathVariable String fileName, @RequestParam("video_id") int video_id,
                                            @RequestParam("eps_id") int eps_id)
            throws IOException {
        // 根据文件名获取相应的 TS 文件内容或进行其他处理
        QueryWrapper<VideoAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("video_id",video_id).eq("eps_id",eps_id);
        VideoAddress videoAddress = videoAddressMapper.selectOne(wrapper) ;
        if(videoAddress != null) {

            String address = videoAddress.getAddress() + "/" + fileName  ;
            Path tsPath = Paths.get(address) ;
            byte[] m3u8Content = Files.readAllBytes(tsPath);

            return ResponseEntity.ok()
                    .contentLength(m3u8Content.length)
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(m3u8Content);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/video/get_epsids")
    public String getEpsIds(@RequestParam("video_id") int video_id) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("video_id",video_id);
        Video video  = videoMapper.selectOne(wrapper) ;
        return video.getEpsIds() ;
    }

    @GetMapping("/video/get_animes")
    // TODO 或许需要分页处理 , 这块还是加个dto
    public List<Video> getAimes() {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();

        return videoMapper.selectList(wrapper) ;
    }
}
